//
//  VedioPlayTableViewController.m
//  糗事百科
//
//  Created by 奚超明 on 16/6/8.
//  Copyright © 2016年 奚超明. All rights reserved.
//

#import "VedioPlayTableViewController.h"
#import "SettingDetailCell.h"

typedef NS_ENUM(NSUInteger, VedioPlayType) {
    VedioPlayTypeWIFIPlay,
    VedioPlayTypeTapPlay,
    VedioPlayTypeAutoPlay
};

@interface VedioPlayTableViewController ()
/** 视频选项列表数组 */
@property (nonatomic, strong) NSMutableArray *titleArr;
/** 当前选中的cell */
@property (nonatomic, strong) NSIndexPath *selectIndexPath;

@end

@implementation VedioPlayTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"视频列表";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SettingDetailCell class]) bundle:nil] forCellReuseIdentifier:@"settingdetail"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}

#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SettingDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"settingdetail"];
    cell.titleLabel.text = self.titleArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.selectBtn.hidden = YES;
    
    // 取出选中的状态
    NSUInteger type = [[[NSUserDefaults standardUserDefaults] objectForKey:@"vedioPlayType"] integerValue];
    
    if (indexPath == [NSIndexPath indexPathForRow:type inSection:0]) {
        self.selectIndexPath = indexPath;
        cell.selectBtn.hidden = NO;
    }
    
    return cell;
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 让上一个cell取消显示选中状态
    if (self.selectIndexPath) {
        SettingDetailCell *cell = [self.tableView cellForRowAtIndexPath:self.selectIndexPath];
        cell.selectBtn.hidden = YES;
    }
    
    // 取到选中的cell
    SettingDetailCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.selectBtn.hidden = NO;
    
    // 记录当前状态
    self.selectIndexPath = indexPath;
    
    // 将状态存储入沙盒
    if (indexPath.row == 0) {
        [[NSUserDefaults standardUserDefaults] setObject:@(VedioPlayTypeWIFIPlay) forKey:@"vedioPlayType"];
    } else if (indexPath.row == 1) {
        [[NSUserDefaults standardUserDefaults] setObject:@(VedioPlayTypeTapPlay) forKey:@"vedioPlayType"];
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@(VedioPlayTypeAutoPlay) forKey:@"vedioPlayType"];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)titleArr
{
    if (!_titleArr) {
        _titleArr = [@[@"仅在WIFI网络下自动播放", @"点击播放", @"总是自动播放"] mutableCopy];
    }
    return _titleArr;
}

@end
